home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / updates / update14.zoo / lib / scanf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-15  |  11.3 KB  |  543 lines

  1. /* Copyright (C) 1991 Free Software Foundation, Inc.
  2.    This file is part of the GNU C Library.
  3.    
  4.    The GNU C Library is free software; you can redistribute it and/or
  5.    modify it under the terms of the GNU Library General Public License as
  6.    published by the Free Software Foundation; either version 2 of the
  7.    License, or (at your option) any later version.
  8.    
  9.    The GNU C Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU Library General Public
  15.    License along with the GNU C Library; see the file COPYING.LIB.  If
  16.    not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17.    Cambridge, MA 02139, USA.  */
  18.  
  19. /* 
  20.  * adapted for atariST gcc lib
  21.  *
  22.  * NOTES: interface is different from equivalent gnuC lib function
  23.  *        it was munged to match our old _scanf().
  24.  *
  25.  *      if __NO_FLOAT__ is defined then the floating point stuff
  26.  *      gets nuked (for iio*.olb) as per our old scanf.c.
  27.  *
  28.  *  It is very important to read and understand the GNU Library General 
  29.  *  Public License. It specifies rights and conditions that are different
  30.  *  from the GNU copyleft.
  31.  *
  32.  *    ++jrb bammi@cadence.com
  33.  */
  34.  
  35. #include <errno.h>
  36. #include <limits.h>
  37. #include <ctype.h>
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42.  
  43. /* the code assumes this definition, note: traditional <ctype> def
  44.  * of tolower will break the code. Ansi def should be ok.
  45.  */
  46. #ifdef tolower
  47. #  undef tolower
  48. #  define    tolower(c)    (isupper(c) ? (c)^0x20 : (c))
  49. #endif
  50.  
  51. #ifndef __NO_FLOAT__
  52. #  define FLOATS 1
  53. #else
  54. #  define FLOATS 0
  55. #endif
  56.  
  57. #define TEN_MUL(X)    ((((X) << 2) + (X)) << 1)
  58.  
  59. #ifdef    __GNUC__
  60. #define    HAVE_LONGLONG
  61. #define    LONGLONG    long long
  62. #else
  63. #define    LONGLONG    long
  64. #endif
  65.  
  66.  
  67. #define    inchar()    ((c = ((*get)(s))) == EOF ? EOF : (++read_in, c))
  68. #define unchar(c)    (--read_in, (*unget)(c, s))
  69. #define    conv_error()    return((c == EOF || ((*unget)(c, s))), done)
  70. #define input_error()    return(-1)
  71. #define    memory_error()    return((errno = ENOMEM), EOF)
  72.  
  73.  
  74. /* Read formatted input from S according to the format string
  75.    FORMAT, using the argument list in ARG.
  76.    Return the number of assignments made, or -1 for an input error.  */
  77. int _scanf(s, get, unget, format, arg)
  78. register FILE *s;
  79. int (*get) __PROTO((FILE *));
  80. int (*unget) __PROTO((int, FILE *));
  81. const char *format;
  82. va_list arg;
  83. {
  84.     register const char *f = format;
  85.     register char fc;        /* Current character of the format.  */
  86.     register size_t done = 0;    /* Assignments done.  */
  87.     register size_t read_in = 0;    /* Chars read in.  */
  88.     register int c;        /* Last char read.  */
  89.     register int do_assign;    /* Whether to do an assignment.  */
  90.     register int width;        /* Maximum field width.  */
  91.     
  92.     /* Type modifiers.  */
  93.     char is_short, is_long, is_long_double;
  94. #ifdef    HAVE_LONGLONG
  95.     /* We use the `L' modifier for `long long int'.  */
  96. #define    is_longlong    is_long_double
  97. #else
  98. #define    is_longlong    0
  99. #endif
  100. #if FLOATS
  101.     /* Status for reading F-P nums.  */
  102.     char got_dot, got_e;
  103. #endif
  104.     /* If a [...] is a [^...].  */
  105.     char not_in;
  106.     /* Base for integral numbers.  */
  107.     int base;
  108.     /* Integral holding variables.  */
  109.     long int num;
  110.     unsigned long int unum;
  111. #if FLOATS
  112.     /* Floating-point holding variable.  */
  113.     long double fp_num;
  114. #endif
  115.     /* Character-buffer pointer.  */
  116.     register char *str;
  117.     /* Workspace.  */
  118.     char work[200];
  119.     char *w;        /* Pointer into WORK.  */
  120.     
  121.     if ((format == NULL) || (!*format))
  122.     {
  123.     errno = EINVAL;
  124.     input_error();
  125.     }
  126.     
  127. # define decimal ('.')    /* should really come from locale stuff that we dont */
  128.     /* have as yet */
  129.     c = inchar();
  130.     
  131.     /* Run through the format string.  */
  132.     while (*f != '\0')
  133.     {
  134. #if 0 /* no mb support as yet */
  135.     if (!isascii(*f))
  136.     {
  137.         /* Non-ASCII, may be a multibyte.  */
  138.         int len = mblen(f, strlen(f));
  139.         if (len > 0)
  140.         {
  141.         while (len-- > 0)
  142.             if (c == EOF)
  143.             input_error();
  144.             else if (c == *f++)
  145.             (void) inchar();
  146.             else
  147.             conv_error();
  148.         continue;
  149.         }
  150.         }
  151. #endif      
  152.     
  153.     fc = *f++;
  154.     if (fc != '%')
  155.     {
  156.         /* Characters other than format specs must just match.  */
  157.         if (c == EOF)
  158.         input_error();
  159.         if (isspace(fc))
  160.         {
  161.         /* Whitespace characters match any amount of whitespace.  */
  162.         while (isspace (c))
  163.             inchar ();
  164.         continue;
  165.         }
  166.         else if (c == fc)
  167.         (void) inchar();
  168.         else
  169.         conv_error();
  170.         continue;
  171.     }
  172.     
  173.     /* Check for the assignment-suppressant.  */
  174.     if (*f == '*')
  175.     {
  176.         do_assign = 0;
  177.         ++f;
  178.     }
  179.     else
  180.         do_assign = 1;
  181.     
  182.     /* Find the maximum field width.  */
  183.     width = 0;
  184.     while (isdigit(*f))
  185.     {
  186.         width *= 10;
  187.         width += *f++ - '0';
  188.     }
  189.     if (width == 0)
  190.         width = -1;
  191.     
  192.     /* Check for type modifiers.  */
  193.     is_short = is_long = is_long_double = 0;
  194.     while (*f == 'h' || *f == 'l' || *f == 'L')
  195.         switch (*f++)
  196.         {
  197.           case 'h':
  198.         /* int's are short int's.  */
  199.         is_short = 1;
  200.         break;
  201.           case 'l':
  202. #ifdef HAVE_LONGLONG
  203.         if (is_long)
  204.             /* A double `l' is equivalent to an `L'.  */
  205.             is_longlong = 1;
  206.         else
  207. #endif
  208.             /* int's are long int's.  */
  209.             is_long = 1;
  210.         break;
  211.           case 'L':
  212.         /* double's are long double's, and int's are long long int's.  */
  213.         is_long_double = 1;
  214.         break;
  215.         }
  216.     
  217.     /* End of the format string?  */
  218.     if (*f == '\0')
  219.         conv_error();
  220.     
  221.     /* Find the conversion specifier.  */
  222.     w = work;
  223.     fc = *f++;
  224.     if (fc != '[' && fc != 'c' && fc != 'n')
  225.         /* Eat whitespace.  */
  226.         while (isspace(c))
  227.         (void) inchar();
  228.     switch (fc)
  229.     {
  230.       case '%':    /* Must match a literal '%'.  */
  231.         if (c != fc)
  232.         conv_error();
  233.         break;
  234.         
  235.       case 'n':    /* Answer number of assignments done.  */
  236.         if (do_assign)
  237.         *va_arg(arg, int *) = read_in - 1; /* -1 is debatable ++jrb */
  238.         break;
  239.         
  240.       case 'c':    /* Match characters.  */
  241.         if (do_assign)
  242.         {
  243.         str = va_arg(arg, char *);
  244.         if (str == NULL)
  245.             conv_error();
  246.         }
  247.         
  248.         if (c == EOF)
  249.         input_error();
  250.         
  251.         if (width == -1)
  252.         width = 1;
  253.         
  254.         if (do_assign)
  255.         while (inchar() != EOF && width-- > 0)
  256.             *str++ = c;
  257.         else
  258.         while (inchar() != EOF && width-- > 0)
  259.             ;
  260.         
  261.         if (do_assign)
  262.         ++done;
  263.         
  264.         if (c == EOF)
  265.         input_error();
  266.         break;
  267.         
  268.       case 's':    /* Read a string.  */
  269.         if (do_assign)
  270.         {
  271.         str = va_arg(arg, char *);
  272.         if (str == NULL)
  273.             conv_error();
  274.         }
  275.         
  276.         if (c == EOF)
  277.         input_error();
  278.         
  279.         do
  280.         {
  281.         if (isspace(c))
  282.             break;
  283.         if (do_assign)
  284.             *str++ = c;
  285.         if (width > 0 && --width == 0)
  286.             break;
  287.         } while (inchar() != EOF);
  288.         
  289.         if (do_assign)
  290.         {
  291.         *str = '\0';
  292.         ++done;
  293.         }
  294.         
  295.         if (c == EOF)
  296.         input_error();
  297.         break;
  298.         
  299.       case 'x':    /* Hexadecimal integer.  */
  300.       case 'X':    /* Ditto.  */ 
  301.         base = 16;
  302.         goto number;
  303.         
  304.       case 'o':    /* Octal integer.  */
  305.         base = 8;
  306.         goto number;
  307.         
  308.       case 'u':    /* Decimal integer.  */
  309.       case 'd':    /* Ditto.  */
  310.         base = 10;
  311.         goto number;
  312.         
  313.       case 'i':    /* Generic number.  */
  314.         base = 0;
  315.         
  316.       number:;
  317.         if (c == EOF)
  318.         input_error();
  319.         
  320.         /* Check for a sign.  */
  321.         if (c == '-' || c == '+')
  322.         {
  323.         *w++ = c;
  324.         if (width > 0)
  325.             --width;
  326.         (void) inchar();
  327.         }
  328.         
  329.         /* Look for a leading indication of base.  */
  330.         if (c == '0')
  331.         {
  332.         if (width > 0)
  333.             --width;
  334.         *w++ = '0';
  335.         
  336.         (void) inchar();
  337.         
  338.         if (tolower(c) == 'x')
  339.         {
  340.             /* one char look ahead to see if its really a lead ind */
  341.             int savec = c;
  342.             int peekc = inchar();
  343.             c = savec;
  344.             (void)unchar(peekc);
  345.             if(isxdigit(peekc))
  346.                     {  
  347.             if (base == 0)
  348.                 base = 16;
  349.             if (base == 16)
  350.             {
  351.                 if (width > 0)
  352.                 --width;
  353.                 (void) inchar();
  354.             }
  355.             }
  356.         }
  357.         else if (base == 0)
  358.             if((c >= '0') && (c <= '7')) 
  359.             base = 8;
  360.         }
  361.         
  362.         if (base == 0)
  363.         base = 10;
  364.         
  365.         /* Read the number into WORK.  */
  366.         do
  367.         {
  368.         if (base == 16 ? !isxdigit(c) :
  369.             (!isdigit(c) || ((c - '0') >= base)))
  370.             break;
  371.         *w++ = c;
  372.         if (width > 0)
  373.             --width;
  374.         } while (inchar() != EOF && width != 0);
  375.         
  376.         if (w == work ||
  377.         (w - work == 1 && (work[0] == '+' || work[0] == '-')))
  378.         /* There was no number.  */
  379.         conv_error();
  380.         
  381.         /* Convert the number.  */
  382.         *w = '\0';
  383.         num = strtol(work, &w, base);
  384.         if (w == work)
  385.         conv_error();
  386.         
  387.         if (do_assign)
  388.         {
  389.         if (is_longlong)
  390.             *va_arg(arg, LONGLONG int *) = num;
  391.         else if (is_long)
  392.             *va_arg(arg, long int *) = num;
  393.         else if (is_short)
  394.             *va_arg(arg, short int *) = (short int) num;
  395.         else
  396.             *va_arg(arg, int *) = (int) num;
  397.         ++done;
  398.         }
  399.         break;
  400.         
  401. #if FLOATS
  402.       case 'e':    /* Floating-point numbers.  */
  403.       case 'E':
  404.       case 'f':
  405.       case 'g':
  406.       case 'G':
  407.         if (c == EOF)
  408.         input_error();
  409.         
  410.         /* Check for a sign.  */
  411.         if (c == '-' || c == '+')
  412.         {
  413.         *w++ = c;
  414.         if (inchar() == EOF)
  415.             input_error();
  416.         if (width > 0)
  417.             --width;
  418.         }
  419.         
  420.         got_dot = got_e = 0;
  421.         do
  422.         {
  423.         if (isdigit(c))
  424.             *w++ = c;
  425.         else if (got_e && w[-1] == 'e' && (c == '-' || c == '+'))
  426.             *w++ = c;
  427.         else if (!got_e && tolower(c) == 'e')
  428.         {
  429.             *w++ = 'e';
  430.             got_e = got_dot = 1;
  431.         }
  432.         else if (c == decimal && !got_dot)
  433.         {
  434.             *w++ = c;
  435.             got_dot = 1;
  436.         }
  437.         else
  438.             break;
  439.         if (width > 0)
  440.             --width;
  441.         } while (inchar() != EOF && width != 0);
  442.         
  443.         if (w == work)
  444.         conv_error();
  445. #if 0
  446.         if (w[-1] == '-' || w[-1] == '+' || w[-1] == 'e')
  447.         conv_error();
  448. #else
  449.         if (w[-1] == '-' || w[-1] == '+')
  450.         conv_error();
  451.  
  452.         if(tolower(w[-1]) == 'e')
  453.         {
  454.         unchar(c);
  455.             --w;
  456.             c = *w;
  457.         }
  458. #endif        
  459.         /* Convert the number.  */
  460.         *w = '\0';
  461.         fp_num = strtod(work, &w);
  462.         if (w == work)
  463.         conv_error();
  464.         
  465.         if (do_assign)
  466.         {
  467.         if (is_long_double)
  468.             *va_arg(arg, long double *) = fp_num;
  469.         else if (is_long)
  470.             *va_arg(arg, double *) = (double) fp_num;
  471.         else
  472.             *va_arg(arg, float *) = (float) fp_num;
  473.         ++done;
  474.         }
  475.         break;
  476. #endif /* FLOATS */
  477.         
  478.       case '[':    /* Character class.  */
  479.         if (do_assign)
  480.         {
  481.         str = va_arg(arg, char *);
  482.         if (str == NULL)
  483.             conv_error();
  484.         }
  485.         
  486.         if (c == EOF)
  487.         input_error();
  488.         
  489.         if (*f == '^')
  490.         {
  491.         ++f;
  492.         not_in = 1;
  493.         }
  494.         else
  495.         not_in = 0;
  496.         
  497.         while ((fc = *f++) != '\0' && fc != ']')
  498.         {
  499.         if (fc == '-' && *f != '\0' && *f != ']' &&
  500.             w > work && w[-1] <= *f)
  501.             /* Add all characters from the one before the '-'
  502.                up to (but not including) the next format char.  */
  503.             for (fc = w[-1] + 1; fc < *f; ++fc)
  504.             *w++ = fc;
  505.         else
  506.             /* Add the character to the list.  */
  507.             *w++ = fc;
  508.         }
  509.         if (fc == '\0')
  510.         conv_error();
  511.         
  512.         *w = '\0';
  513.         unum = read_in;
  514.         do
  515.         {
  516.         if ((strchr(work, c) == NULL) != not_in)
  517.             break;
  518.         if (do_assign)
  519.             *str++ = c;
  520.         if (width > 0)
  521.             --width;
  522.         } while (inchar() != EOF && width != 0);
  523.         if (read_in == unum)
  524.         conv_error();
  525.         
  526.         if (do_assign)
  527.         {
  528.         *str = '\0';
  529.         ++done;
  530.         }
  531.         break;
  532.         
  533.       case 'p':    /* Generic pointer.  */
  534.         base = 16;
  535.         /* A PTR must be the same size as a `long int'.  */
  536.         is_long = 1;
  537.         goto number;
  538.     }
  539.     }
  540.     
  541.     conv_error();
  542. }
  543.